import scala.collection.immutable.List
import scala.collection.mutable.HashMap
package dataflowVariety{

class Equation(termData:List[(String,Double)]) 
{
  //initializing equation data
  var data = HashMap[String,Double]()
  
  //order
  var order = termData.size-1
  
  for (t<-termData) data(t._1) = t._2
  
  def getOrder = order
  
  def solve(updatedVars:List[String],caMap:HashMap[String,(CustomAttribute,HashMap[Int,List[Equation]])]):String = 
  {
    //return value
    var varName:String = null
    var inter = data -- updatedVars
    inter -= "$"
    if (inter.size == 1) 
    {
      var finalValue:Double = 0
     
      for (i<-inter) varName = i._1
      var myCoef = data(varName)
      for (v <-data)
      {
        if (v._1 != varName)
        {
          if (v._1 == "$")
          {
            finalValue -= (v._2/myCoef)
          }
          else
          {
            finalValue -= (v._2 * caMap(v._1)._1.getVal.toDouble / myCoef)
          }
        }
      }
      
      //updating
      caMap(varName)._1.setVal(finalValue.toString())
    }
    
    varName;
  }
  
  def solvable(updatedVars:List[String]):Boolean = 
  {
    var inter = data -- updatedVars
    inter -= "$"
    if (inter.size == 1) return true
    else return false      
  }

}
}